/********************************************
 * 功能说明: Tenant实体与DTO之间接口数据转换实现
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.converter;

import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.common.idgenerator.IdGenerator;
import com.spring.arch.uaa.enums.Constant;
import com.spring.arch.uaa.enums.TenantStatus;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.web.dto.WebTenantDTO;
import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.common.idgenerator.IdGenerator;
import com.spring.arch.uaa.enums.Constant;
import com.spring.arch.uaa.enums.TenantStatus;
import com.spring.arch.uaa.model.Tenant;
import com.spring.arch.uaa.model.TenantEmployee;
import com.spring.arch.uaa.repository.TenantEmployeeRepository;
import com.spring.arch.uaa.repository.TenantRepository;
import com.spring.arch.uaa.web.dto.WebTenantDTO;
import lombok.NonNull;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001001001;

/**
 * Tenant实体与DTO之间接口数据转换实现
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Component
public class WebTenantConverter extends AbstractConverter<Tenant, WebTenantDTO> {

    @Autowired
    private TenantRepository tenantRepository;
    @Autowired
    private WebTenantEmployeeConverter tenantEmployeeConverter;
    @Autowired
    private TenantEmployeeRepository tenantEmployeeRepository;

    @Autowired
    private EntityManager entityManager;

    @Override
    public Tenant toModel(@NonNull final WebTenantDTO dto) {
        if (dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public WebTenantDTO toDTO(@NonNull final Tenant model) {
        final WebTenantDTO dto = new WebTenantDTO();

        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setAbbreviation(model.getAbbreviation());
        dto.setContact(model.getContact());
        dto.setMobile(model.getMobile());
        dto.setDomain(model.getDomain());
        dto.setDomainIdentity(model.getDomainIdentity());
        dto.setLogo(model.getLogo());
        dto.setStatus(model.getStatus());

        // 获取租户信息
        TenantEmployee employee = tenantEmployeeRepository.findByCode(Constant.tenantAdminCode(model.getCode()));
        if (employee != null) {
            dto.setTenantAdminDTO(tenantEmployeeConverter.toDTO(employee));
        }
        this.convertCommonToDTO(model, dto);

        return dto;
    }

    /**
     * 构建新Model
     */
    private Tenant constructModel(final WebTenantDTO dto) {
        final Tenant model = new Tenant();
        model.setCode(IdGenerator.uniqueCode());
        model.setStatus(TenantStatus.ENABLE);
        this.dtoToModel(dto, model);
        return model;
    }

    /**
     * 更新Model
     */
    private Tenant updateModel(final WebTenantDTO dto) {
        final Tenant model = this.tenantRepository.findById(dto.getId())
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001001001, Tenant.class.getSimpleName()));
        entityManager.unwrap(Session.class).evict(model);
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final WebTenantDTO dto, final Tenant model) {

        model.setName(dto.getName());
        model.setAbbreviation(dto.getAbbreviation());
        // model.setRootOrganization(dto.getRootOrganization());
        model.setContact(dto.getContact());
        model.setMobile(dto.getMobile());
        model.setDomain(dto.getDomain());
        model.setDomainIdentity(dto.getDomainIdentity());
        model.setLogo(dto.getLogo());

    }
}
